import { TaskRule } from "../../rules/TaskRule";
import { ModuleBg } from "../common/ModuleBg";
import { ResouceConst } from "../../../component/gconst/ResouceConst";
import { TaskTree } from "./tree/TaskTree";
import { GScrollPane } from "../../../../com/mui/controls/GScrollPane";
import { ScrollPolicy } from "../../../../fl/controls/ScrollPolicy";
import { TaskTreeBranch } from "./tree/TaskTreeBranch";
import { InvalidationType } from "../../../../fl/core/InvalidationType";
import { TaskTreeNode } from "./tree/TaskTreeNode";
import { DataEvent } from "../../events/DataEvent";
import { ObjectPool } from "../../../../com/gengine/utils/pools/ObjectPool";
import { TaskInfo } from "../../model/TaskInfo";
type int = number;
//class TaskListPanel
    
    export  class TaskListPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this.taskTypeData = TaskRule.getTaskGroupTitleMap();
            this._bg = new ModuleBg();
            this._bg.addItem(ResouceConst.getScaleBitmap("WindowCenterB"), {"x": 0, "y": 0, "width": 165, "height": 302});
            addChild(this._bg);
            this._bodyList = new TaskTree();
            this._bodyList.addEventListener(flash.events.MouseEvent.CLICK, this.onTreeClickHandler);
            this._scrollPane = new GScrollPane();
            this._scrollPane.x = 2;
            this._scrollPane.y = 2;
            this._scrollPane.width = 162;
            this._scrollPane.height = 300;
            this._scrollPane.source = this._bodyList;
            this._scrollPane.verticalScrollPolicy = ScrollPolicy.AUTO;
            this._scrollPane.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._scrollPane.styleName = "GScrollPane";
            this._scrollPane.setScrollBarSize(19);
            addChild(this._scrollPane);
            return;
        }

        public show(): void
        {
            this._scrollPane.update();
            return;
        }

        /* internal  */onTreeClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=null;
            var loc1=arg1.target;
            if (typeof loc1 === "tasktreebranch") 
            {
                (loc1 as TaskTreeBranch).treeChange();
                this._bodyList.bodyList.layChildChanged = true;
                this._bodyList.bodyList.invalidate(InvalidationType.ALL);
                this._scrollPane.update();
            }
            else if (typeof loc1 === "tasktreenode") 
            {
                loc2 = loc1 as TaskTreeNode;
                if (!(this._lastNodeSelected == null) && !(this._lastNodeSelected.task == null) && loc2.task.sTask.code == this._lastNodeSelected.task.sTask.code && loc2.task.createDt == this._lastNodeSelected.task.createDt) 
                {
                    return;
                }
                loc2.selected();
                if (this._lastNodeSelected) 
                {
                    this._lastNodeSelected.unSelected();
                }
                this._lastNodeSelected = loc2;
                dispatchEvent(new DataEvent("task_tree_selected", loc2.task));
            }
            return;
        }

        public initTaskList(arg1: Array<any>): void
        {
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=0;
            this.dispose();
            this._scrollPane.verticalScrollPosition = 0;
            var loc1=0;
            var loc2=this.taskTypeData.length;
            var loc3=[];
            while (loc1 < loc2) 
            {
                loc4 = this.taskTypeData[loc1];
                loc3.splice(0);
                loc6 = 0;
                loc7 = arg1.length;
                while (loc6 < loc7) 
                {
                    if (arg1[loc6].sTask.group == loc4.type) 
                    {
                        loc3.push(arg1[loc6]);
                    }
                    ++loc6;
                }
                if (loc3.length > 0) 
                {
                    (loc5 = ObjectPool.getObject(TaskTreeBranch)).initData(loc4, loc3);
                    this._bodyList.bodyList.addChild(loc5);
                }
                ++loc1;
            }
            this._bodyList.bodyList.layChildChanged = true;
            this._bodyList.bodyList.invalidate(InvalidationType.ALL);
            this._scrollPane.update();
            return;
        }

        public insertTask(arg1: TaskInfo): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=null;
            var loc2=this._bodyList.bodyList.numChildren;
            while (loc1 < loc2) 
            {
                if ((loc3 = this._bodyList.bodyList.getChildAt(loc1) as TaskTreeBranch) && loc3.type == arg1.sTask.group) 
                {
                    loc3.insertTask(arg1, null);
                    this._bodyList.bodyList.layChildChanged = true;
                    this._bodyList.bodyList.invalidate(InvalidationType.ALL);
                    this._scrollPane.update();
                    return;
                }
                ++loc1;
            }
            loc1 = 0;
            loc2 = this.taskTypeData.length;
            while (loc1 < loc2) 
            {
                if ((loc4 = this.taskTypeData[loc1]).type == arg1.sTask.group) 
                {
                    (loc3 = ObjectPool.getObject(TaskTreeBranch)).insertTask(arg1, loc4);
                    this._bodyList.bodyList.addChildAt(loc3, Math.min(loc1, this._bodyList.bodyList.numChildren));
                    break;
                }
                ++loc1;
            }
            this._bodyList.bodyList.layChildChanged = true;
            this._bodyList.bodyList.invalidate(InvalidationType.ALL);
            this._scrollPane.update();
            return;
        }

        public delTask(arg1: int): void
        {
            var loc3=null;
            var loc4=0;
            var loc1=0;
            var loc2=this._bodyList.bodyList.numChildren;
            while (loc1 < loc2) 
            {
                if ((loc4 = (loc3 = this._bodyList.bodyList.getChildAt(loc1) as TaskTreeBranch).delTask(arg1)) == arg1) 
                {
                    if (loc3.bodyLength == 0) 
                    {
                        loc3.dispose();
                        this._bodyList.bodyList.removeChild(loc3);
                        ObjectPool.disposeObject(loc3, TaskTreeBranch);
                    }
                    this._bodyList.bodyList.layChildChanged = true;
                    this._bodyList.bodyList.invalidate(InvalidationType.ALL);
                    if (this._lastNodeSelected && this._lastNodeSelected.taskId == loc4) 
                    {
                        this._lastNodeSelected = null;
                    }
                    break;
                }
                ++loc1;
            }
            this._scrollPane.update();
            return;
        }

        public updateTask(arg1: TaskInfo): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=0;
            var loc2=this._bodyList.bodyList.numChildren;
            while (loc1 < loc2) 
            {
                if ((loc4 = (loc3 = this._bodyList.bodyList.getChildAt(loc1) as TaskTreeBranch).updateTask(arg1)) == arg1.sTask.code) 
                {
                    break;
                }
                ++loc1;
            }
            return;
        }

        public dispose(): void
        {
            var loc1=null;
            while (this._bodyList.bodyList.numChildren > 0) 
            {
                loc1 = this._bodyList.bodyList.removeChildAt(0) as TaskTreeBranch;
                loc1.dispose();
                ObjectPool.disposeObject(loc1, TaskTreeBranch);
            }
            return;
        }

        /* internal */ /* var */taskTypeData: Array<any>;

        /* internal */ /* var */_bg: ModuleBg;

        /* internal */ /* var */_scrollPane: GScrollPane;

        /* internal */ /* var */_bodyList: TaskTree;

        /* internal */ /* var */_lastNodeSelected: TaskTreeNode;
    }
